package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.seckill.SeckillFeignCilent;
import com.atguigu.gmall.feign.user.UserFeignCilent;
import com.atguigu.gmall.order.entity.OrderInfo;
import com.atguigu.gmall.seckill.entity.SeckillGoods;
import com.atguigu.gmall.user.entity.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Description
 * @Return
 * @Author https://weiranyi.github.io
 * @Date Created in 2022-12-26 08:46
 **/
@Controller
public class SeckillController {
    @Autowired
    SeckillFeignCilent seckillFeignCilent;
    @Autowired
    UserFeignCilent userFeignCilent;

    /**
     * http://activity.gmall.com/seckill.html
     * 去秒杀首页,展示所有秒杀山坡
     *
     * @return
     */
    @GetMapping("/seckill.html")
    public String toSeckillPage(Model model) {
        List<SeckillGoods> goods = seckillFeignCilent.getTodaySeckillGoods().getData();
        model.addAttribute("list", goods);
        return "seckill/index";
    }

    /**
     * http://activity.gmall.com/seckill/50.html
     * 去详情页，展示某个秒杀商品
     *
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    public String seckillDetail(@PathVariable("skuId") Long skuId,
                                Model model) {
        // 默认走本地缓存，速度极快
        SeckillGoods goods = seckillFeignCilent.getSeckillGoodsDetails(skuId).getData();
        model.addAttribute("item", goods);

        return "seckill/item";
    }

    /**
     * 秒杀排队页
     *
     * @param skuId
     * @param skuIdStr
     * @param model
     * @return
     */
    // http://activity.gmall.com/seckill/queue.html?skuId=50&skuIdStr=b94c77f9066dbc8350ac9254c3858e20
    @GetMapping("/seckill/queue.html")
    public String queue(@RequestParam("skuId") Long skuId,
                        @RequestParam("skuIdStr") String skuIdStr,
                        Model model) {
        model.addAttribute("skuId", skuId);
        model.addAttribute("skuIdStr", skuIdStr);
        return "seckill/queue";
    }

    // http://activity.gmall.com/seckill/trade.html?skuId=50
    @GetMapping("/seckill/trade.html")
    public String trade(@RequestParam("skuId") Long skuId, Model model) {
        Result<List<UserAddress>> userAddresses = userFeignCilent.getUserAddress();
        OrderInfo orderInfo = seckillFeignCilent.seckillOrderInfo(skuId).getData();

        model.addAttribute("userAddressList", userAddresses.getData());
        model.addAttribute("detailArrayList", orderInfo.getOrderDetails());
        model.addAttribute("totalNum", "1");
        model.addAttribute("totalAmount", orderInfo.getTotalAmount());

        return "seckill/trade";
    }
}
